import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

const STATUS_ID = "status"

export default new Vuex.Store({
    state: {
        status: localStorage.getItem(STATUS_ID),
        user: {},
        navs: '',
        Max: 6000,
        currentRoolRoom: JSON.parse(sessionStorage.getItem('currentRoolRoom'))
    },
    mutations: {
        setMax(state, Max) {
            state.Max = Max;
        },
        setCurrentRollRoom(state, query) {
            sessionStorage.setItem('currentRoolRoom', JSON.stringify(query))
            state.currentRoolRoom = query
        },
        signin_request(state) {
            state.status = 'loading'
        },
        signin_success(state, user) {
            state.status = 'success'
            state.user = user
        },
        signin_navs(state, k) {
            state.navs = k
        },
        signin_animation(state, an) {
            state.user.animation = an
        },
        signin_money(state, mo) {
            state.user.money = mo
        },
        // 修改steam
        signin_steam(state, k) {
            state.user.steamStorehouseLink = k
        },
        signin_soundEffects(state, sf) {
            state.user.soundEffects = sf
        },
        signin_error(state) {
            state.status = 'error'
        },
        signout(state) {
            state.status = localStorage.removeItem(STATUS_ID)
            state.user = {}
        },
    },
    actions: {
        signin({ commit, dispatch }, user) {
            return new Promise((resolve, reject) => {
                commit('signin_request')

                //网络请求
                // if 成功
                commit('signin_success', response.user)
                    // else
                commit('signin_error')

            })
        },
        signup({ commit }, user) {
            return new Promise((resolve, reject) => {
                //网络请求
            })
        },
        signout({ commit }) {
            return new Promise((resolve, reject) => {
                //网络请求
                commit('signout')
            })
        },
    },
    getters: {
        isSignedIn: state => state.status == 'success',
    }
})